import { Processor, IProcessor } from '@midwayjs/bull';
import {  FORMAT, Inject } from '@midwayjs/decorator';
import { RedisService } from '@midwayjs/redis';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { NovelEntity } from 'src/entity/novel/novel';

import { NovelPartEntity } from 'src/entity/part/part';
import { StatisticEntity } from 'src/entity/Statistics/Statistics';
import { Repository } from 'typeorm';


@Processor('UpdataView', {

  repeat: {
    // cron: FORMAT.CRONTAB.EVERY_PER_30_SECOND
   cron:FORMAT.CRONTAB.EVERY_HOUR // 每小时更新数据库浏览记录
  }
})
export class UpdataView implements IProcessor {


  @Inject()
  redisService: RedisService;

  @InjectEntityModel(NovelEntity)
  NovelModel: Repository<NovelEntity>;
  @InjectEntityModel(NovelPartEntity)
  NovelPartModel: Repository<NovelPartEntity>;
  @InjectEntityModel(StatisticEntity)
  StatisticEntitytModel: Repository<StatisticEntity>;


  async execute() {
//  记录浏览量
            this.redisService.hgetall('novel:view').then(async res =>{
                if(JSON.stringify(res) =="{}"){
                    return
                }
                this.redisService.hdel('novel:view',...Object.keys(res))
                    // 得到对象 对数据库浏览量进行添加
                    //  记录总量 
                    var num = 0
                    for(let  key in  res){
                        num =num +Number(res[key])
                        this.NovelModel.createQueryBuilder().update()
                        .set( { browse: () => "browse + "+res[key]} )
                        .where({ id: key  })
                        .execute();
                    }

                    // 插入数据库   计算今日浏览量
                    var  tiem = Date.now()
                    console.log(tiem);
                    
                let reslut   = await  this.StatisticEntitytModel.createQueryBuilder().update()
                    .set(   { viewtotal: () => "viewtotal + "+num}     )
                    .where( ` TO_DAYS(CreateDate) = TO_DAYS(NOW())`   )
                    .execute();
                    if(reslut.affected ==0){
                        await  this.StatisticEntitytModel.save( { viewtotal: num })
                    }
                    


                
            } )
            this.redisService.hgetall('novel:sort').then(res =>{
                // this.redisService.hdel('novel:sort')
                if(JSON.stringify(res) =="{}"){
                    return
                }
                this.redisService.hdel('novel:sort',...Object.keys(res))
                console.log("hash",res);
                    // 得到对象 对数据库浏览量进行添加
                    for(let  key in  res){
                        // console.log("浏览量" ,res[key]);
                        // console.log("浏览量" , key.split('/') );
                        var target =  key.split('/')
                        this.NovelPartModel.createQueryBuilder().update()
                        .set( { Views: () => "Views + "+res[key]} )
                        .where({ id: target[1] , Pid:target[0]  })
                        .execute();
                    }
                
            } )
   
    

       
  }
}